/*
 * @(#)AuditTrail.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.audit.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Vector;

import com.dcivision.audit.AuditTrailConstant;
import com.dcivision.audit.bean.AuditTrail;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsRoot;
import com.dcivision.dms.web.ListDmsAuditTrailForm;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.DataSourceFactory;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.framework.web.ListPersonalHomeForm;

/**
  AuditTrailDAObject.java
  This class is the data access bean for table "AUDIT_TRAIL".
  @author      Zoe Shum
  @company     DCIVision Limited
  @creation date   11/09/2003
  @version     $Revision: 1.45.2.10 $
 */
/* ********************************Modify History***********************************************
 * Modify by  : Jicle.Wang 
 * Modify date: 2006/08/28
 * BugID      : 6035
 * Description: Shows Audit Trail of File(s) or Folder(s) be soft_deleted in RecentDocuments(s)
 * *********************************************************************************************
 */
public class AuditTrailDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.45.2.10 $";

  public static final String TABLE_NAME = "AUDIT_TRAIL";

  public AuditTrailDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  
  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("OBJECT_TYPE");
    this.vecDBColumn.add("OBJECT_ID");
    this.vecDBColumn.add("OBJECT_NAME");
    this.vecDBColumn.add("VERSION_NUMBER");
    this.vecDBColumn.add("ACCESS_TYPE");
    this.vecDBColumn.add("SESSION_ID");
    this.vecDBColumn.add("IP_ADDRESS");
    this.vecDBColumn.add("ACCESSOR_ID");
    this.vecDBColumn.add("ACCESS_DATE");
  }

  
  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized (dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.OBJECT_TYPE, A.OBJECT_ID, A.OBJECT_NAME, A.VERSION_NUMBER, A.ACCESS_TYPE, A.SESSION_ID, A.IP_ADDRESS, A.ACCESSOR_ID, A.ACCESS_DATE ");
        sqlStat.append("FROM   AUDIT_TRAIL A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
                                          ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          AuditTrail tmpAuditTrail = new AuditTrail();
          tmpAuditTrail.setID(getResultSetInteger(rs, "ID"));
          tmpAuditTrail.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpAuditTrail.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpAuditTrail.setObjectName(getResultSetString(rs, "OBJECT_NAME"));
          tmpAuditTrail.setVersionNumber(getResultSetString(rs, "VERSION_NUMBER"));
          tmpAuditTrail.setAccessType(getResultSetString(rs, "ACCESS_TYPE"));
          tmpAuditTrail.setSessionID(getResultSetString(rs, "SESSION_ID"));
          tmpAuditTrail.setIpAddress(getResultSetString(rs, "IP_ADDRESS"));
          tmpAuditTrail.setAccessorID(getResultSetInteger(rs, "ACCESSOR_ID"));
          tmpAuditTrail.setAccessDate(getResultSetTimestamp(rs, "ACCESS_DATE"));
          tmpAuditTrail.setCreatorName(UserInfoFactory.getUserFullName(tmpAuditTrail.getCreatorID()));
          tmpAuditTrail.setUpdaterName(UserInfoFactory.getUserFullName(tmpAuditTrail.getUpdaterID()));
          return (tmpAuditTrail);
        }
        else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try {
          rs.close();
        } catch (Exception ignore) {} finally {
          rs = null;
        }
        try {
          preStat.close();
        } catch (Exception ignore) {} finally {
          preStat = null;
        }
      }
    }
  }

  
  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());
    ListDmsAuditTrailForm actionForm = (ListDmsAuditTrailForm)searchForm;

    synchronized (dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.OBJECT_TYPE, A.OBJECT_ID, A.OBJECT_NAME, A.VERSION_NUMBER, A.ACCESS_TYPE, A.SESSION_ID, A.IP_ADDRESS, A.ACCESSOR_ID, A.ACCESS_DATE ");
        sqlStat.append("FROM   AUDIT_TRAIL A ");
        sqlStat.append("WHERE  A.OBJECT_TYPE = ? ");

        // Form the WHERE clause for filtering.      
        if (!Utility.isEmpty(actionForm.getObjectID())) {
          sqlStat.append(" AND A.OBJECT_ID = ? ");
        }

        if (DataSourceFactory.getDatabaseType() == DataSourceFactory.DB_ORACLE) {
          if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
            sqlStat.append("AND (A.ACCESS_DATE >=TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(actionForm.getAccessDateFrom()),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS') AND A.ACCESS_DATE <=TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(actionForm.getAccessDateTo()),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS') ) ");
          }
          else if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && Utility.isEmpty(actionForm.getAccessDateTo())) {
            sqlStat.append("AND (A.ACCESS_DATE >= TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(actionForm.getAccessDateFrom()),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS') ) ");
          }
          else if (Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
            sqlStat.append("AND (A.ACCESS_DATE <= TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(TextUtility.dbDateAdder(actionForm.getAccessDateTo(),1)),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS')  ) ");
          }
        }else{
          if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
            sqlStat.append("AND (A.ACCESS_DATE >= '"+actionForm.getAccessDateFrom()+"' AND A.ACCESS_DATE <= '"+TextUtility.dbDateAdder(actionForm.getAccessDateTo(),1)+"' ) ");
          }else if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && Utility.isEmpty(actionForm.getAccessDateTo())) {
            sqlStat.append("AND (A.ACCESS_DATE >= '"+actionForm.getAccessDateFrom()+"' ) ");
          }else if (Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
            sqlStat.append("AND (A.ACCESS_DATE <= '"+TextUtility.dbDateAdder(actionForm.getAccessDateTo(),1)+"'  ) ");
          }
        }

        if (!Utility.isEmpty(actionForm.getAccessType())) {
          sqlStat.append("AND A.ACCESS_TYPE = ? ");
        }       
        
        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        int i = 0;
        this.setPrepareStatement(preStatCnt, ++i, actionForm.getObjectType());

        // Form the WHERE clause for filtering.
        if (!Utility.isEmpty(actionForm.getObjectID())) {
          this.setPrepareStatement(preStatCnt, ++i, actionForm.getObjectID());
        }

        if (!Utility.isEmpty(actionForm.getAccessType())) {
          this.setPrepareStatement(preStatCnt, ++i, actionForm.getAccessType());
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try {
          rsCnt.close();
        } catch (Exception ignore) {} finally {
          rsCnt = null;
        }
        try {
          preStatCnt.close();
        } catch (Exception ignore) {} finally {
          preStatCnt = null;
        }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
                                          ResultSet.CONCUR_READ_ONLY);
        i = 0;
        this.setPrepareStatement(preStat, ++i, actionForm.getObjectType());
        // Form the WHERE clause for filtering.        
        if (!Utility.isEmpty(actionForm.getObjectID())) {
          this.setPrepareStatement(preStat, ++i, actionForm.getObjectID());
        }

        if (!Utility.isEmpty(actionForm.getAccessType())) {
          this.setPrepareStatement(preStat, ++i, actionForm.getAccessType());
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt < pageSize) {
          AuditTrail tmpAuditTrail = new AuditTrail();
          tmpAuditTrail.setID(getResultSetInteger(rs, "ID"));
          tmpAuditTrail.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpAuditTrail.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpAuditTrail.setObjectName(getResultSetString(rs, "OBJECT_NAME"));
          tmpAuditTrail.setVersionNumber(getResultSetString(rs, "VERSION_NUMBER"));
          tmpAuditTrail.setAccessType(getResultSetString(rs, "ACCESS_TYPE"));
          tmpAuditTrail.setSessionID(getResultSetString(rs, "SESSION_ID"));
          tmpAuditTrail.setIpAddress(getResultSetString(rs, "IP_ADDRESS"));
          tmpAuditTrail.setAccessorID(getResultSetInteger(rs, "ACCESSOR_ID"));
          tmpAuditTrail.setAccessDate(getResultSetTimestamp(rs, "ACCESS_DATE"));
          tmpAuditTrail.setCreatorName(UserInfoFactory.getUserFullName(tmpAuditTrail.getCreatorID()));
          tmpAuditTrail.setUpdaterName(UserInfoFactory.getUserFullName(tmpAuditTrail.getUpdaterID()));
          tmpAuditTrail.setRecordCount(totalNumOfRecord);
          tmpAuditTrail.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpAuditTrail);
        }
        return (result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try {
          rs.close();
        } catch (Exception ignore) {} finally {
          rs = null;
        }
        try {
          preStat.close();
        } catch (Exception ignore) {} finally {
          preStat = null;
        }
        try {
          rsCnt.close();
        } catch (Exception ignore) {} finally {
          rsCnt = null;
        }
        try {
          preStatCnt.close();
        } catch (Exception ignore) {} finally {
          preStatCnt = null;
        }
      }
    }
  }
  
  
  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized (dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.OBJECT_TYPE, A.OBJECT_ID, A.OBJECT_NAME, A.VERSION_NUMBER, A.ACCESS_TYPE, A.SESSION_ID, A.IP_ADDRESS, A.ACCESSOR_ID, A.ACCESS_DATE ");
        sqlStat.append("FROM   AUDIT_TRAIL A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
                                          ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          AuditTrail tmpAuditTrail = new AuditTrail();
          tmpAuditTrail.setID(getResultSetInteger(rs, "ID"));
          tmpAuditTrail.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpAuditTrail.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpAuditTrail.setObjectName(getResultSetString(rs, "OBJECT_NAME"));
          tmpAuditTrail.setVersionNumber(getResultSetString(rs, "VERSION_NUMBER"));
          tmpAuditTrail.setAccessType(getResultSetString(rs, "ACCESS_TYPE"));
          tmpAuditTrail.setSessionID(getResultSetString(rs, "SESSION_ID"));
          tmpAuditTrail.setIpAddress(getResultSetString(rs, "IP_ADDRESS"));
          tmpAuditTrail.setAccessorID(getResultSetInteger(rs, "ACCESSOR_ID"));
          tmpAuditTrail.setAccessDate(getResultSetTimestamp(rs, "ACCESS_DATE"));
          tmpAuditTrail.setCreatorName(UserInfoFactory.getUserFullName(tmpAuditTrail.getCreatorID()));
          tmpAuditTrail.setUpdaterName(UserInfoFactory.getUserFullName(tmpAuditTrail.getUpdaterID()));
          result.add(tmpAuditTrail);
        }
        return (result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try {
          rs.close();
        } catch (Exception ignore) {} finally {
          rs = null;
        }
        try {
          preStat.close();
        } catch (Exception ignore) {} finally {
          preStat = null;
        }
      }
    }
  }

  
  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    AuditTrail tmpAuditTrail = (AuditTrail) ( (AuditTrail)obj).clone();

    synchronized (dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   AUDIT_TRAIL(ID, OBJECT_TYPE, OBJECT_ID, OBJECT_NAME, VERSION_NUMBER, ACCESS_TYPE, SESSION_ID, IP_ADDRESS, ACCESSOR_ID, ACCESS_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
                                          ResultSet.CONCUR_READ_ONLY);
        int i = 1;
        setPrepareStatement(preStat, i++, nextID);
        setPrepareStatement(preStat, i++, tmpAuditTrail.getObjectType());
        setPrepareStatement(preStat, i++, tmpAuditTrail.getObjectID());
        setPrepareStatement(preStat, i++, tmpAuditTrail.getObjectName());
        setPrepareStatement(preStat, i++, tmpAuditTrail.getVersionNumber());
        setPrepareStatement(preStat, i++, tmpAuditTrail.getAccessType());
        setPrepareStatement(preStat, i++, tmpAuditTrail.getSessionID());
        setPrepareStatement(preStat, i++, tmpAuditTrail.getIpAddress());
        setPrepareStatement(preStat, i++, tmpAuditTrail.getAccessorID());
        setPrepareStatement(preStat, i++, currTime);
        preStat.executeUpdate();
        
        tmpAuditTrail.setID(nextID);
        tmpAuditTrail.setCreatorID(sessionContainer.getUserRecordID());
        tmpAuditTrail.setCreateDate(currTime);
        tmpAuditTrail.setUpdaterID(sessionContainer.getUserRecordID());
        tmpAuditTrail.setUpdateDate(currTime);
        tmpAuditTrail.setUpdateCount(new Integer(0));
        tmpAuditTrail.setCreatorName(UserInfoFactory.getUserFullName(tmpAuditTrail.getCreatorID()));
        tmpAuditTrail.setUpdaterName(UserInfoFactory.getUserFullName(tmpAuditTrail.getUpdaterID()));
        return (tmpAuditTrail);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try {
          preStat.close();
        } catch (Exception ignore) {} finally {
          preStat = null;
        }
      }
    }
  }

  
  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    AuditTrail tmpAuditTrail = (AuditTrail) ( (AuditTrail)obj).clone();

    synchronized (dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE AUDIT_TRAIL ");
        sqlStat.append("SET  OBJECT_TYPE=?, OBJECT_ID=?, OBJECT_NAME=?, VERSION_NUMBER=?, ACCESS_TYPE=?, SESSION_ID=?, IP_ADDRESS=?, ACCESSOR_ID=?, ACCESS_DATE=? ");
        sqlStat.append("WHERE  ID=?  ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
                                          ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpAuditTrail.getObjectType());
        setPrepareStatement(preStat, 2, tmpAuditTrail.getObjectID());
        setPrepareStatement(preStat, 3, tmpAuditTrail.getObjectName());
        setPrepareStatement(preStat, 4, tmpAuditTrail.getVersionNumber());
        setPrepareStatement(preStat, 5, tmpAuditTrail.getAccessType());
        setPrepareStatement(preStat, 6, tmpAuditTrail.getSessionID());
        setPrepareStatement(preStat, 7, tmpAuditTrail.getIpAddress());
        setPrepareStatement(preStat, 8, tmpAuditTrail.getAccessorID());
        setPrepareStatement(preStat, 9, tmpAuditTrail.getAccessDate());
        setPrepareStatement(preStat, 10, tmpAuditTrail.getID());

        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        }
        else {
          tmpAuditTrail.setUpdaterID(sessionContainer.getUserRecordID());
          tmpAuditTrail.setUpdateDate(currTime);
          tmpAuditTrail.setUpdateCount(new Integer(tmpAuditTrail.getUpdateCount().intValue() + 1));
          tmpAuditTrail.setCreatorName(UserInfoFactory.getUserFullName(tmpAuditTrail.getCreatorID()));
          tmpAuditTrail.setUpdaterName(UserInfoFactory.getUserFullName(tmpAuditTrail.getUpdaterID()));
          return (tmpAuditTrail);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try {
          preStat.close();
        } catch (Exception ignore) {} finally {
          preStat = null;
        }
      }
    }
  }

  
  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    AuditTrail tmpAuditTrail = (AuditTrail) ( (AuditTrail)obj).clone();

    synchronized (dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   AUDIT_TRAIL ");
        sqlStat.append("WHERE  ID=?  ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
                                          ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpAuditTrail.getID());

        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        }
        else {
          return (tmpAuditTrail);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try {
          preStat.close();
        } catch (Exception ignore) {} finally {
          preStat = null;
        }
      }
    }
  }

  
  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    AuditTrail tmpAuditTrail = (AuditTrail)this.oldValue;

    if (tmpAuditTrail != null) {
      oldValues.add(toAuditTrailValue(tmpAuditTrail.getID()));
      oldValues.add(toAuditTrailValue(tmpAuditTrail.getObjectType()));
      oldValues.add(toAuditTrailValue(tmpAuditTrail.getObjectID()));
      oldValues.add(toAuditTrailValue(tmpAuditTrail.getVersionNumber()));
      oldValues.add(toAuditTrailValue(tmpAuditTrail.getAccessType()));
      oldValues.add(toAuditTrailValue(tmpAuditTrail.getSessionID()));
      oldValues.add(toAuditTrailValue(tmpAuditTrail.getIpAddress()));
      oldValues.add(toAuditTrailValue(tmpAuditTrail.getAccessorID()));
      oldValues.add(toAuditTrailValue(tmpAuditTrail.getAccessDate()));
    }

    tmpAuditTrail = (AuditTrail)obj;
    if (tmpAuditTrail != null) {
      newValues.add(toAuditTrailValue(tmpAuditTrail.getID()));
      newValues.add(toAuditTrailValue(tmpAuditTrail.getObjectType()));
      newValues.add(toAuditTrailValue(tmpAuditTrail.getObjectID()));
      newValues.add(toAuditTrailValue(tmpAuditTrail.getVersionNumber()));
      newValues.add(toAuditTrailValue(tmpAuditTrail.getAccessType()));
      newValues.add(toAuditTrailValue(tmpAuditTrail.getSessionID()));
      newValues.add(toAuditTrailValue(tmpAuditTrail.getIpAddress()));
      newValues.add(toAuditTrailValue(tmpAuditTrail.getAccessorID()));
      newValues.add(toAuditTrailValue(tmpAuditTrail.getAccessDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/

  public synchronized List getDmsAccessList(String accessList, AbstractSearchForm form) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    PreparedStatement preStat1 = null;
    ResultSet rs = null;
    ResultSet rs1 = null;
    ResultSet rsCnt = null;
    StringBuffer oriSQLStat = new StringBuffer();
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    String inAuditTrailIDList = null;
    List result = new ArrayList();
    ListPersonalHomeForm listHomeForm = (ListPersonalHomeForm)form;
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(listHomeForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(form.getPageOffset());
    Timestamp currTime = Utility.getCurrentTimestamp();

    synchronized (dbConn) {
      try {
        oriSQLStat.append("SELECT MAX(A.ID) AS ID, A.OBJECT_ID ");
        oriSQLStat.append("FROM   AUDIT_TRAIL A, DMS_DOCUMENT D, DMS_ROOT R ");
        oriSQLStat.append("WHERE  A.OBJECT_TYPE = ? ");
        if (GlobalConstant.HOME_RECENTLY_ACCESS_LIST.equals(accessList)) {
          oriSQLStat.append("     AND A.ACCESSOR_ID = ? ");
          oriSQLStat.append("     AND (R.ROOT_TYPE = ? OR (R.ROOT_TYPE = ? AND R.OWNER_ID= ? )) ");          
        }
        oriSQLStat.append("       AND A.OBJECT_ID = D.ID ");
        oriSQLStat.append("       AND R.RECORD_STATUS = ? ");
        //joan.xiong add code for bug EIP-1083 by 2006/11/27
        oriSQLStat.append("       AND D.RECORD_STATUS <> 'P' ");
        //oriSQLStat.append("       AND D.RECORD_STATUS = ? ");//Update By Jim Zhou     2006/12/15    EIP-1173
        //joan.xiong add code end
        oriSQLStat.append("       AND D.ROOT_ID > 0 ");
        oriSQLStat.append("       AND D.ROOT_ID = R.ID ");
        oriSQLStat.append("       AND D.PARENT_ID > 0 ");
        oriSQLStat.append("       AND R.ROOT_TYPE = 'U' ");
        oriSQLStat.append(" AND ((D.EFFECTIVE_START_DATE IS NULL OR D.EFFECTIVE_START_DATE <= ?) OR D.OWNER_ID = ?) ");
        
        /* Jicle.Wang 6035 2006/08/28 Start */
        //oriSQLStat.append("       AND D.RECORD_STATUS <> ? ");                               
        //oriSQLStat.append("       AND D.RECORD_STATUS <> ? ");
        /* Jicle.Wang 6035 2006/08/28 End   */
        oriSQLStat.append("GROUP BY A.OBJECT_ID ");
        oriSQLStat.append("ORDER BY ID DESC ");
        
        sqlStatCnt = this.getSelectCountSQL(oriSQLStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        int i = 0;
        this.setPrepareStatement(preStatCnt, ++i, GlobalConstant.OBJECT_TYPE_DOCUMENT);
        if (GlobalConstant.HOME_RECENTLY_ACCESS_LIST.equals(accessList)) {
          this.setPrepareStatement(preStatCnt, ++i, sessionContainer.getUserRecordID());
          this.setPrepareStatement(preStatCnt, ++i, DmsRoot.PUBLIC_ROOT);
          this.setPrepareStatement(preStatCnt, ++i, DmsRoot.PERSONAL_ROOT);
          this.setPrepareStatement(preStatCnt, ++i, sessionContainer.getUserRecordID());
        }
        this.setPrepareStatement(preStatCnt, ++i, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStatCnt, ++i, currTime);
        this.setPrepareStatement(preStatCnt, ++i, sessionContainer.getUserRecordID());
        
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try {
          rsCnt.close();
        } catch (Exception ignore) {} finally {
          rsCnt = null;
        }
        try {
          preStatCnt.close();
        } catch (Exception ignore) {} finally {
          preStatCnt = null;
        }
        
        i = 0;
        oriSQLStat = this.getSelectListSQL(oriSQLStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(oriSQLStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);        
        this.setPrepareStatement(preStat, ++i, GlobalConstant.OBJECT_TYPE_DOCUMENT);
        if (GlobalConstant.HOME_RECENTLY_ACCESS_LIST.equals(accessList)) {
          this.setPrepareStatement(preStat, ++i, sessionContainer.getUserRecordID());
          this.setPrepareStatement(preStat, ++i, DmsRoot.PUBLIC_ROOT);
          this.setPrepareStatement(preStat, ++i, DmsRoot.PERSONAL_ROOT);
          this.setPrepareStatement(preStat, ++i, sessionContainer.getUserRecordID());
        }
        this.setPrepareStatement(preStat, ++i, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, ++i, currTime);
        this.setPrepareStatement(preStat, ++i, sessionContainer.getUserRecordID());
        
        
        rs = preStat.executeQuery();
        this.positionCursor(rs, startOffset, pageSize);
        
       // while (rs.next() && rowLoopCnt<pageSize) {//delete by tc
        while(rs.next()){
          if (Utility.isEmpty(inAuditTrailIDList)) {
            inAuditTrailIDList = rs.getString("ID");
          } else {
            inAuditTrailIDList += "," + rs.getString("ID");
          }
          ++rowLoopCnt;
        }
        
        if (Utility.isEmpty(inAuditTrailIDList)) {
          return (result);
        }
        
        sqlStat.append("SELECT DISTINCT(A.OBJECT_ID), A.ID, A.OBJECT_TYPE, A.VERSION_NUMBER, A.ACCESS_TYPE, A.SESSION_ID, A.IP_ADDRESS, A.ACCESSOR_ID, A.ACCESS_DATE, ");
        sqlStat.append("D.DOCUMENT_NAME, D.DOCUMENT_TYPE, D.PARENT_ID, D.ROOT_ID, D.PRIORITY, D.CREATE_TYPE, D.EFFECTIVE_START_DATE, D.EFFECTIVE_END_DATE, D.REMINDER_AMOUNT, D.REMINDER_TYPE, D.NOTIFICATION_WAY, D.OWNER_ID, D.REFERENCE_NO, D.DESCRIPTION, D.ITEM_SIZE, D.ITEM_STATUS, D.CHECKOUT_ID, D.ALERT_EDIT_ID, D.ALERT_INVITE_ID, D.USER_DEF_1, D.USER_DEF_2, D.USER_DEF_3, D.USER_DEF_4, D.USER_DEF_5, D.USER_DEF_6, D.USER_DEF_7, D.USER_DEF_8, D.USER_DEF_9, D.USER_DEF_10, D.RECORD_STATUS, D.UPDATE_COUNT, D.CREATOR_ID, D.CREATE_DATE, D.UPDATER_ID, D.UPDATE_DATE, R.RECORD_STATUS AS ROOT_RECORD_STATUS ");
        sqlStat.append("FROM   AUDIT_TRAIL A, DMS_DOCUMENT D, DMS_ROOT R ");
        sqlStat.append("WHERE  A.ID IN (" + inAuditTrailIDList + ") AND A.OBJECT_ID = D.ID AND D.ROOT_ID = R.ID "); 
        sqlStat.append("ORDER BY A.ID DESC ");
        

       
        sqlStat=this.getSelectListSQL(sqlStat, startOffset, pageSize);//ADD By TC
        preStat1 = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        
        
        rs1 = preStat1.executeQuery();
        this.positionCursor(rs1, startOffset, pageSize);//ADD BY TC
        while (rs1.next()) {
          DmsDocument tmpDmsDocument = new DmsDocument();
          tmpDmsDocument.setID(getResultSetInteger(rs1, "OBJECT_ID"));
          tmpDmsDocument.setDocumentName(getResultSetString(rs1, "DOCUMENT_NAME"));
          tmpDmsDocument.setDocumentType(getResultSetString(rs1, "DOCUMENT_TYPE"));
          tmpDmsDocument.setParentID(getResultSetInteger(rs1, "PARENT_ID"));
          tmpDmsDocument.setRootID(getResultSetInteger(rs1, "ROOT_ID"));
          tmpDmsDocument.setCreateType(getResultSetString(rs1, "CREATE_TYPE"));
          tmpDmsDocument.setEffectiveStartDate(getResultSetTimestamp(rs1, "EFFECTIVE_START_DATE"));
          tmpDmsDocument.setEffectiveEndDate(getResultSetTimestamp(rs1, "EFFECTIVE_END_DATE"));
          tmpDmsDocument.setOwnerID(getResultSetInteger(rs1, "OWNER_ID"));
          tmpDmsDocument.setReferenceNo(getResultSetString(rs1, "REFERENCE_NO"));
          tmpDmsDocument.setDescription(getResultSetString(rs1, "DESCRIPTION"));
          tmpDmsDocument.setItemSize(getResultSetInteger(rs1, "ITEM_SIZE"));
          tmpDmsDocument.setItemStatus(getResultSetString(rs1, "ITEM_STATUS"));
          tmpDmsDocument.setCheckoutID(getResultSetInteger(rs1, "CHECKOUT_ID"));
          tmpDmsDocument.setAlertEditID(getResultSetInteger(rs1, "ALERT_EDIT_ID"));
          tmpDmsDocument.setAlertInviteID(getResultSetInteger(rs1, "ALERT_INVITE_ID"));
          tmpDmsDocument.setUserDef1(getResultSetString(rs1, "USER_DEF_1"));
          tmpDmsDocument.setUserDef2(getResultSetString(rs1, "USER_DEF_2"));
          tmpDmsDocument.setUserDef3(getResultSetString(rs1, "USER_DEF_3"));
          tmpDmsDocument.setUserDef4(getResultSetString(rs1, "USER_DEF_4"));
          tmpDmsDocument.setUserDef5(getResultSetString(rs1, "USER_DEF_5"));
          tmpDmsDocument.setUserDef6(getResultSetString(rs1, "USER_DEF_6"));
          tmpDmsDocument.setUserDef7(getResultSetString(rs1, "USER_DEF_7"));
          tmpDmsDocument.setUserDef8(getResultSetString(rs1, "USER_DEF_8"));
          tmpDmsDocument.setUserDef9(getResultSetString(rs1, "USER_DEF_9"));
          tmpDmsDocument.setUserDef10(getResultSetString(rs1, "USER_DEF_10"));
          tmpDmsDocument.setRecordStatus(getResultSetString(rs1, "RECORD_STATUS"));
          tmpDmsDocument.setUpdateCount(getResultSetInteger(rs1, "UPDATE_COUNT"));
          tmpDmsDocument.setCreatorID(getResultSetInteger(rs1, "CREATOR_ID"));
          tmpDmsDocument.setCreateDate(getResultSetTimestamp(rs1, "CREATE_DATE"));
          tmpDmsDocument.setUpdaterID(getResultSetInteger(rs1, "UPDATER_ID"));
          tmpDmsDocument.setUpdateDate(getResultSetTimestamp(rs1, "UPDATE_DATE"));
          tmpDmsDocument.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDocument.getCreatorID()));
          tmpDmsDocument.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDocument.getUpdaterID()));
          tmpDmsDocument.setAccessDate(getResultSetTimestamp(rs1, "ACCESS_DATE"));
          tmpDmsDocument.setAccessType(getResultSetString(rs1, "ACCESS_TYPE"));
          tmpDmsDocument.setActionUserID(getResultSetInteger(rs1, "ACCESSOR_ID"));
          tmpDmsDocument.setRecordCount(totalNumOfRecord);
          tmpDmsDocument.setRowNum(startOffset++);
          result.add(tmpDmsDocument);
        }

        return (result);
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rs1.close(); } catch (Exception ignore) {} finally { rs1 = null; }
        try { preStat1.close(); } catch (Exception ignore) {} finally { preStat1 = null; }
      }
    }
  }   
  
  /**
   * Get audit trail record list by user id.
   * 
   * @param searchForm
   * @return
   * @throws ApplicationException
   */
  public synchronized List getListByUserID(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());
    ListDmsAuditTrailForm actionForm = (ListDmsAuditTrailForm)searchForm;
    Timestamp currTime = Utility.getCurrentTimestamp();
    
    synchronized (dbConn) {
      try {
    	sqlStat.append("SELECT A.ID, A.OBJECT_TYPE, A.OBJECT_ID, A.OBJECT_NAME, A.VERSION_NUMBER, A.ACCESS_TYPE, A.SESSION_ID, A.IP_ADDRESS, A.ACCESSOR_ID, A.ACCESS_DATE ");
    	sqlStat.append("FROM   AUDIT_TRAIL A ,DMS_DOCUMENT D, DMS_ROOT R ");
    	sqlStat.append("WHERE  A.OBJECT_TYPE = ?  ");
    	sqlStat.append("AND A.OBJECT_ID = D.ID AND D.ROOT_ID = R.ID ");
        if(!Utility.isEmpty(actionForm.getUserRecordID())){
            sqlStat.append("AND A.ACCESSOR_ID="+actionForm.getUserRecordID()+" ");
        }
        // Form the WHERE clause for filtering.
        if (!Utility.isEmpty(actionForm.getObjectID())) {
          sqlStat.append(" AND A.OBJECT_ID = ? ");
        }
        if (DataSourceFactory.getDatabaseType() == DataSourceFactory.DB_ORACLE) {
            if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
               sqlStat.append("AND (A.ACCESS_DATE >=TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(actionForm.getAccessDateFrom()),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS') AND A.ACCESS_DATE <=TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(actionForm.getAccessDateTo()),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS') ) ");
             }
             else if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && Utility.isEmpty(actionForm.getAccessDateTo())) {
               sqlStat.append("AND (A.ACCESS_DATE >= TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(actionForm.getAccessDateFrom()),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS') ) ");
             }
             else if (Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
               sqlStat.append("AND (A.ACCESS_DATE <= TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(TextUtility.dbDateAdder(actionForm.getAccessDateTo(),1)),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS')  ) ");
             }
        }else{
        	if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
               sqlStat.append("AND (A.ACCESS_DATE >= '"+actionForm.getAccessDateFrom()+"' AND A.ACCESS_DATE <= '"+TextUtility.dbDateAdder(actionForm.getAccessDateTo(),1)+"' ) ");
             }
             else if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && Utility.isEmpty(actionForm.getAccessDateTo())) {
               sqlStat.append("AND (A.ACCESS_DATE >= '"+actionForm.getAccessDateFrom()+"' ) ");
             }
             else if (Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
               sqlStat.append("AND (A.ACCESS_DATE <= '"+TextUtility.dbDateAdder(actionForm.getAccessDateTo(),1)+"'  ) ");
             }
        }
        if (!Utility.isEmpty(actionForm.getAccessType())) {
            sqlStat.append("AND A.ACCESS_TYPE = '"+actionForm.getAccessType()+"' ");
        }
        if (!Utility.isEmpty(actionForm.getRootID()) && !Utility.isEmpty(actionForm.getParentID())){
        	sqlStat.append("AND D.ROOT_ID = ? ");
        	sqlStat.append("AND D.PARENT_ID = ? ");
        }
        
        sqlStat.append("       AND D.RECORD_STATUS <> 'P' ");
        sqlStat.append(" AND R.ROOT_TYPE = 'U' ");
        
        sqlStat.append(" AND ((D.EFFECTIVE_START_DATE IS NULL OR D.EFFECTIVE_START_DATE <= ?) OR D.OWNER_ID = ?) ");

          // Form the ORDER clause for sorting.
	      if (searchForm.isSortable()) {
	        String sortAttribute = searchForm.getSortAttribute();
	        if (sortAttribute.indexOf(".") < 0) {
	          sortAttribute = "A." + sortAttribute;
	        }
	        sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
	      }
        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        
        int i = 0;
        this.setPrepareStatement(preStatCnt, ++i, actionForm.getObjectType());
        // Form the WHERE clause for filtering.
        if (!Utility.isEmpty(actionForm.getObjectID())) {
          this.setPrepareStatement(preStatCnt, ++i, actionForm.getObjectID());
        }

        if (!Utility.isEmpty(actionForm.getRootID()) && !Utility.isEmpty(actionForm.getParentID())){
        	this.setPrepareStatement(preStatCnt, ++i, actionForm.getRootID());
        	this.setPrepareStatement(preStatCnt, ++i, actionForm.getParentID());
        }

        this.setPrepareStatement(preStatCnt, ++i, currTime);
        this.setPrepareStatement(preStatCnt, ++i, sessionContainer.getUserRecordID());

        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try {
          rsCnt.close();
        } catch (Exception ignore) {} finally {
          rsCnt = null;
        }
        try {
          preStatCnt.close();
        } catch (Exception ignore) {} finally {
          preStatCnt = null;
        }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
                                          ResultSet.CONCUR_READ_ONLY);
        i = 0;
        this.setPrepareStatement(preStat, ++i, actionForm.getObjectType());
        
        // Form the WHERE clause for filtering.
        if (!Utility.isEmpty(actionForm.getObjectID())) {
          this.setPrepareStatement(preStat, ++i, actionForm.getObjectID());
        }
        if (!Utility.isEmpty(actionForm.getRootID()) && !Utility.isEmpty(actionForm.getParentID())){
        	this.setPrepareStatement(preStat, ++i, actionForm.getRootID());
        	this.setPrepareStatement(preStat, ++i, actionForm.getParentID());
        }
        this.setPrepareStatement(preStat, ++i, currTime);
        this.setPrepareStatement(preStat, ++i, sessionContainer.getUserRecordID());

        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt < pageSize) {
          AuditTrail tmpAuditTrail = new AuditTrail();
          tmpAuditTrail.setID(getResultSetInteger(rs, "ID"));
          tmpAuditTrail.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpAuditTrail.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpAuditTrail.setObjectName(getResultSetString(rs, "OBJECT_NAME"));
          tmpAuditTrail.setVersionNumber(getResultSetString(rs, "VERSION_NUMBER"));
          tmpAuditTrail.setAccessType(getResultSetString(rs, "ACCESS_TYPE"));
          tmpAuditTrail.setSessionID(getResultSetString(rs, "SESSION_ID"));
          tmpAuditTrail.setIpAddress(getResultSetString(rs, "IP_ADDRESS"));
          tmpAuditTrail.setAccessorID(getResultSetInteger(rs, "ACCESSOR_ID"));
          tmpAuditTrail.setAccessDate(getResultSetTimestamp(rs, "ACCESS_DATE"));
          tmpAuditTrail.setCreatorName(UserInfoFactory.getUserFullName(tmpAuditTrail.getCreatorID()));
          tmpAuditTrail.setUpdaterName(UserInfoFactory.getUserFullName(tmpAuditTrail.getUpdaterID()));
          tmpAuditTrail.setRecordCount(totalNumOfRecord);
          tmpAuditTrail.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpAuditTrail);
        }
        return (result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try {
          rs.close();
        } catch (Exception ignore) {} finally {
          rs = null;
        }
        try {
          preStat.close();
        } catch (Exception ignore) {} finally {
          preStat = null;
        }
        try {
          rsCnt.close();
        } catch (Exception ignore) {} finally {
          rsCnt = null;
        }
        try {
          preStatCnt.close();
        } catch (Exception ignore) {} finally {
          preStatCnt = null;
        }
      }
    }
  }
  
  /**
   * Get audit trail record list by user id and document ids.
   * 
   * @param searchForm
   * @param objectIDs
   * @return
   * @throws ApplicationException
   */
  public synchronized List getListByUserID(AbstractSearchForm searchForm, List objectIDs) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());
    ListDmsAuditTrailForm actionForm = (ListDmsAuditTrailForm)searchForm;
    Timestamp currTime = Utility.getCurrentTimestamp();
    
    synchronized (dbConn) {
      try {
      sqlStat.append("SELECT A.ID, A.OBJECT_TYPE, A.OBJECT_ID, A.OBJECT_NAME, A.VERSION_NUMBER, A.ACCESS_TYPE, A.SESSION_ID, A.IP_ADDRESS, A.ACCESSOR_ID, A.ACCESS_DATE ");

      if(DataSourceFactory.getDatabaseType() == DataSourceFactory.DB_MYSQL) {
        sqlStat.append("FROM   AUDIT_TRAIL A LEFT JOIN (DMS_DOCUMENT D, DMS_ROOT R) ");
        sqlStat.append(" ON (A.OBJECT_ID = D.ID AND D.ROOT_ID = R.ID) ");
      } else {
        sqlStat.append("FROM   AUDIT_TRAIL A LEFT JOIN DMS_DOCUMENT D ON A.OBJECT_ID = D.ID ");
        sqlStat.append(" LEFT JOIN  DMS_ROOT R ON D.ROOT_ID = R.ID ");
      }
      
      sqlStat.append("WHERE  A.OBJECT_TYPE = ?  ");
        if(!Utility.isEmpty(actionForm.getUserRecordID())){
            sqlStat.append("AND A.ACCESSOR_ID="+actionForm.getUserRecordID()+" ");
        }
        if (DataSourceFactory.getDatabaseType() == DataSourceFactory.DB_ORACLE) {
            if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
               sqlStat.append("AND (A.ACCESS_DATE >=TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(actionForm.getAccessDateFrom()),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS') AND A.ACCESS_DATE <=TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(actionForm.getAccessDateTo()),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS') ) ");
             }
             else if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && Utility.isEmpty(actionForm.getAccessDateTo())) {
               sqlStat.append("AND (A.ACCESS_DATE >= TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(actionForm.getAccessDateFrom()),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS') ) ");
             }
             else if (Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
               sqlStat.append("AND (A.ACCESS_DATE <= TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(TextUtility.dbDateAdder(actionForm.getAccessDateTo(),1)),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS')  ) ");
             }
        }else{
          if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
               sqlStat.append("AND (A.ACCESS_DATE >= '"+actionForm.getAccessDateFrom()+"' AND A.ACCESS_DATE <= '"+TextUtility.dbDateAdder(actionForm.getAccessDateTo(),1)+"' ) ");
             }
             else if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && Utility.isEmpty(actionForm.getAccessDateTo())) {
               sqlStat.append("AND (A.ACCESS_DATE >= '"+actionForm.getAccessDateFrom()+"' ) ");
             }
             else if (Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
               sqlStat.append("AND (A.ACCESS_DATE <= '"+TextUtility.dbDateAdder(actionForm.getAccessDateTo(),1)+"'  ) ");
             }
        }
        if (!Utility.isEmpty(actionForm.getAccessType())) {
            sqlStat.append("AND A.ACCESS_TYPE = '"+actionForm.getAccessType()+"' ");
        }
        
        if (!Utility.isEmpty(objectIDs)){
          if (objectIDs.size()>0){
            sqlStat.append(" AND ( ");
          }
          for (int i = 0; i<objectIDs.size(); i++){
            sqlStat.append(" A.OBJECT_ID = ?  OR ");
          }
          if (objectIDs.size()>0){
            sqlStat.delete(sqlStat.length()-3, sqlStat.length()-1);
            sqlStat.append(" ) ");  
          }
        }
        
        sqlStat.append("       AND (D.RECORD_STATUS <> 'P' OR D.RECORD_STATUS IS NULL) ");
        sqlStat.append(" AND (R.ROOT_TYPE = 'U' OR R.ROOT_TYPE IS NULL) ");
        
        sqlStat.append(" AND ((D.EFFECTIVE_START_DATE IS NULL OR D.EFFECTIVE_START_DATE <= ?) OR D.OWNER_ID = ?) ");

          // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }
        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        
        int i = 0;
        this.setPrepareStatement(preStatCnt, ++i, actionForm.getObjectType());

        if (!Utility.isEmpty(objectIDs)){
          for (int x = 0; x<objectIDs.size(); x++){
            this.setPrepareStatement(preStatCnt, ++i, (Integer)objectIDs.get(x));
          }
        }

        this.setPrepareStatement(preStatCnt, ++i, currTime);
        this.setPrepareStatement(preStatCnt, ++i, sessionContainer.getUserRecordID());

        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try {
          rsCnt.close();
        } catch (Exception ignore) {} finally {
          rsCnt = null;
        }
        try {
          preStatCnt.close();
        } catch (Exception ignore) {} finally {
          preStatCnt = null;
        }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
                                          ResultSet.CONCUR_READ_ONLY);
        i = 0;
        this.setPrepareStatement(preStat, ++i, actionForm.getObjectType());

        if (!Utility.isEmpty(objectIDs)){
          for (int x = 0; x<objectIDs.size(); x++){
            this.setPrepareStatement(preStat, ++i, (Integer)objectIDs.get(x));
          }
        }
        
        this.setPrepareStatement(preStat, ++i, currTime);
        this.setPrepareStatement(preStat, ++i, sessionContainer.getUserRecordID());

        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt < pageSize) {
          AuditTrail tmpAuditTrail = new AuditTrail();
          tmpAuditTrail.setID(getResultSetInteger(rs, "ID"));
          tmpAuditTrail.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpAuditTrail.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpAuditTrail.setObjectName(getResultSetString(rs, "OBJECT_NAME"));
          tmpAuditTrail.setVersionNumber(getResultSetString(rs, "VERSION_NUMBER"));
          tmpAuditTrail.setAccessType(getResultSetString(rs, "ACCESS_TYPE"));
          tmpAuditTrail.setSessionID(getResultSetString(rs, "SESSION_ID"));
          tmpAuditTrail.setIpAddress(getResultSetString(rs, "IP_ADDRESS"));
          tmpAuditTrail.setAccessorID(getResultSetInteger(rs, "ACCESSOR_ID"));
          tmpAuditTrail.setAccessDate(getResultSetTimestamp(rs, "ACCESS_DATE"));
          tmpAuditTrail.setCreatorName(UserInfoFactory.getUserFullName(tmpAuditTrail.getCreatorID()));
          tmpAuditTrail.setUpdaterName(UserInfoFactory.getUserFullName(tmpAuditTrail.getUpdaterID()));
          tmpAuditTrail.setRecordCount(totalNumOfRecord);
          tmpAuditTrail.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpAuditTrail);
        }
        return (result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try {
          rs.close();
        } catch (Exception ignore) {} finally {
          rs = null;
        }
        try {
          preStat.close();
        } catch (Exception ignore) {} finally {
          preStat = null;
        }
        try {
          rsCnt.close();
        } catch (Exception ignore) {} finally {
          rsCnt = null;
        }
        try {
          preStatCnt.close();
        } catch (Exception ignore) {} finally {
          preStatCnt = null;
        }
      }
    }
  }

  private Timestamp parseTimestamp(String timeStr) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
      java.util.Date date;
      try {
        date = sdf.parse(timeStr);
      } catch (java.text.ParseException e1) {
        date = sdf.parse(timeStr);
      }
      return new java.sql.Timestamp(date.getTime());
    } catch (java.text.ParseException e) {
      return null;
    }
  }

  public synchronized void deleteListByObjectTypeObjectID(String objectType, Integer objectID) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
 
    synchronized (dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   AUDIT_TRAIL ");
        sqlStat.append("WHERE  OBJECT_TYPE = ? AND OBJECT_ID= ?  ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
                                          ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, objectType);
        setPrepareStatement(preStat, 2, objectID);

        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          //throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        }

      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try {
          preStat.close();
        } catch (Exception ignore) {} finally {
          preStat = null;
        }
      }
    }
  }
  
  public synchronized List getListByUserID(AbstractSearchForm searchForm ,String FolderTreeList) throws ApplicationException {
	    PreparedStatement preStat = null;
	    PreparedStatement preStatCnt = null;
	    ResultSet rs = null;
	    ResultSet rsCnt = null;
	    StringBuffer sqlStat = new StringBuffer();
	    StringBuffer sqlStatCnt = new StringBuffer();
	    List result = new ArrayList();
	    int totalNumOfRecord = 0;
	    int rowLoopCnt = 0;
	    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
	    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());
	    ListDmsAuditTrailForm actionForm = (ListDmsAuditTrailForm)searchForm;
	    synchronized (dbConn) {
	      try {
	        sqlStat.append("SELECT A.ID, A.OBJECT_TYPE, A.OBJECT_ID, A.OBJECT_NAME, A.VERSION_NUMBER, A.ACCESS_TYPE, A.SESSION_ID, A.IP_ADDRESS, A.ACCESSOR_ID, A.ACCESS_DATE ");
	        sqlStat.append("FROM   AUDIT_TRAIL A  ");
	        sqlStat.append("WHERE  A.OBJECT_TYPE = ?  ");
	        if(!Utility.isEmpty(actionForm.getUserRecordID())){
	          sqlStat.append("AND A.ACCESSOR_ID="+actionForm.getUserRecordID()+" ");
	        }
	        // Form the WHERE clause for filtering.
	        if (!Utility.isEmpty(actionForm.getObjectID())) {
	          sqlStat.append(" AND A.OBJECT_ID = ? ");
	        }
	        if (FolderTreeList!=null && !"".equals(FolderTreeList)){
	        	sqlStat.append("and  object_ID in (");
	        	sqlStat.append(FolderTreeList);
	        	sqlStat.append(") ");
	        }
	       if (DataSourceFactory.getDatabaseType() == DataSourceFactory.DB_ORACLE) {
	             if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
	                sqlStat.append("AND (A.ACCESS_DATE >=TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(actionForm.getAccessDateFrom()),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS') AND A.ACCESS_DATE <=TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(actionForm.getAccessDateTo()),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS') ) ");
	              }
	              else if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && Utility.isEmpty(actionForm.getAccessDateTo())) {
	                sqlStat.append("AND (A.ACCESS_DATE >= TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(actionForm.getAccessDateFrom()),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS') ) ");
	              }
	              else if (Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
	                sqlStat.append("AND (A.ACCESS_DATE <= TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(TextUtility.dbDateAdder(actionForm.getAccessDateTo(),1)),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS')  ) ");
	              }
	        }else{
	          if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
	                sqlStat.append("AND (A.ACCESS_DATE >= '"+actionForm.getAccessDateFrom()+"' AND A.ACCESS_DATE <= '"+TextUtility.dbDateAdder(actionForm.getAccessDateTo(),1)+"' ) ");
	              }
	              else if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && Utility.isEmpty(actionForm.getAccessDateTo())) {
	                sqlStat.append("AND (A.ACCESS_DATE >= '"+actionForm.getAccessDateFrom()+"' ) ");
	              }
	              else if (Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
	                sqlStat.append("AND (A.ACCESS_DATE <= '"+TextUtility.dbDateAdder(actionForm.getAccessDateTo(),1)+"'  ) ");
	              }
	        }
	        if (!Utility.isEmpty(actionForm.getAccessType())) {
	          sqlStat.append("AND A.ACCESS_TYPE = '"+actionForm.getAccessType()+"' ");
	        }

	        // Form the ORDER clause for sorting.
	        if (searchForm.isSortable()) {
	          String sortAttribute = searchForm.getSortAttribute();
	          if (sortAttribute.indexOf(".") < 0) {
	            sortAttribute = "A." + sortAttribute;
	          }
	          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
	        }
	        //log.debug("&&&&&&&&&&&&&&&&&&&&sqlStat="+sqlStat);
	        // Get total number of record return.
	       sqlStatCnt = this.getSelectCountSQL(sqlStat);
	        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
	        int i = 0;
	        this.setPrepareStatement(preStatCnt, ++i, actionForm.getObjectType());
	        // Form the WHERE clause for filtering.
	        if (!Utility.isEmpty(actionForm.getObjectID())) {
	          this.setPrepareStatement(preStatCnt, ++i, actionForm.getObjectID());
	        }

	        rsCnt = preStatCnt.executeQuery();
	        //log.debug("---------" + sqlStat);
	        if (rsCnt.next()) {
	          totalNumOfRecord = rsCnt.getInt(1);
	        }
	        try {
	          rsCnt.close();
	        } catch (Exception ignore) {} finally {
	          rsCnt = null;
	        }
	        try {
	          preStatCnt.close();
	        } catch (Exception ignore) {} finally {
	          preStatCnt = null;
	        }

	        // Retrieve the result in row basis.
	        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
	        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
	                                          ResultSet.CONCUR_READ_ONLY);
	        i = 0;
	        this.setPrepareStatement(preStat, ++i, actionForm.getObjectType());
	        /*if(!Utility.isEmpty(actionForm.getUserRecordID())){
	          this.setPrepareStatement(preStatCnt,++i,actionForm.getUserRecordID());
	        }else{
	          this.setPrepareStatement(preStatCnt,++i,sessionContainer.getUserRecordID());
	        }*/
	        // Form the WHERE clause for filtering.
	        if (!Utility.isEmpty(actionForm.getObjectID())) {
	          this.setPrepareStatement(preStat, ++i, actionForm.getObjectID());
	        }

	        rs = preStat.executeQuery();

	        this.positionCursor(rs, startOffset, pageSize);

	        while (rs.next() && rowLoopCnt < pageSize) {
	          AuditTrail tmpAuditTrail = new AuditTrail();
	          tmpAuditTrail.setID(getResultSetInteger(rs, "ID"));
	          tmpAuditTrail.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
	          tmpAuditTrail.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
	          tmpAuditTrail.setObjectName(getResultSetString(rs, "OBJECT_NAME"));
	          tmpAuditTrail.setVersionNumber(getResultSetString(rs, "VERSION_NUMBER"));
	          tmpAuditTrail.setAccessType(getResultSetString(rs, "ACCESS_TYPE"));
	          tmpAuditTrail.setSessionID(getResultSetString(rs, "SESSION_ID"));
	          tmpAuditTrail.setIpAddress(getResultSetString(rs, "IP_ADDRESS"));
	          tmpAuditTrail.setAccessorID(getResultSetInteger(rs, "ACCESSOR_ID"));
	          tmpAuditTrail.setAccessDate(getResultSetTimestamp(rs, "ACCESS_DATE"));
	          tmpAuditTrail.setCreatorName(UserInfoFactory.getUserFullName(tmpAuditTrail.getCreatorID()));
	          tmpAuditTrail.setUpdaterName(UserInfoFactory.getUserFullName(tmpAuditTrail.getUpdaterID()));
	          tmpAuditTrail.setRecordCount(totalNumOfRecord);
	          tmpAuditTrail.setRowNum(startOffset++);
	          ++rowLoopCnt;
	          result.add(tmpAuditTrail);
	        }
	        return (result);
	      } catch (ApplicationException appEx) {
	        throw appEx;
	      } catch (SQLException sqle) {
	        log.error(sqle, sqle);
	        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
	      } catch (Exception e) {
	        log.error(e, e);
	        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
	      } finally {
	        try {
	          rs.close();
	        } catch (Exception ignore) {} finally {
	          rs = null;
	        }
	        try {
	          preStat.close();
	        } catch (Exception ignore) {} finally {
	          preStat = null;
	        }
	        try {
	          rsCnt.close();
	        } catch (Exception ignore) {} finally {
	          rsCnt = null;
	        }
	        try {
	          preStatCnt.close();
	        } catch (Exception ignore) {} finally {
	          preStatCnt = null;
	        }
	      }
	    }
	  }
  
  public synchronized List getListByDocumentType(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MONTH,-1);
    String startTime = TextUtility.formatDate(cal.getTime(), SystemParameterFactory.getSystemParameter(SystemParameterConstant.DB_DATETIME_FORMAT));

    synchronized (dbConn) {
      try {
        sqlStat.append("SELECT DISTINCT A.ID, A.OBJECT_TYPE, A.OBJECT_ID, A.OBJECT_NAME, A.VERSION_NUMBER, A.ACCESS_TYPE, A.SESSION_ID, A.IP_ADDRESS, A.ACCESSOR_ID, A.ACCESS_DATE ");
        sqlStat.append("FROM   AUDIT_TRAIL A, DMS_DOCUMENT B ");
        sqlStat.append("WHERE  A.OBJECT_TYPE = ? AND A.OBJECT_ID = B.ID AND B.DOCUMENT_TYPE = ? AND B.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        
        sqlStat.append("AND (A.ACCESS_DATE >= ? ) ");    
        sqlStat.append("AND (A.ACCESSOR_ID = ? ) ");
        
        // Form the ORDER clause for sorting.
//        if (searchForm.isSortable()) {
//          String sortAttribute = searchForm.getSortAttribute();
//          if (sortAttribute.indexOf(".") < 0) {
//            sortAttribute = "A." + sortAttribute;
//          }
//          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
//        }
        
        sqlStat.append("ORDER BY ACCESS_DATE DESC");

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        int i = 0;
        this.setPrepareStatement(preStatCnt, ++i, GlobalConstant.OBJECT_TYPE_DOCUMENT);
        this.setPrepareStatement(preStatCnt, ++i, DmsDocument.FOLDER_TYPE);
        this.setPrepareStatement(preStatCnt, ++i, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStatCnt, ++i, startTime);
        this.setPrepareStatement(preStatCnt, ++i, sessionContainer.getUserRecordID());
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try {
          rsCnt.close();
        } catch (Exception ignore) {} finally {
          rsCnt = null;
        }
        try {
          preStatCnt.close();
        } catch (Exception ignore) {} finally {
          preStatCnt = null;
        }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
                                          ResultSet.CONCUR_READ_ONLY);
        i = 0;
        this.setPrepareStatement(preStat, ++i, GlobalConstant.OBJECT_TYPE_DOCUMENT);
        this.setPrepareStatement(preStat,++i,DmsDocument.FOLDER_TYPE);
        this.setPrepareStatement(preStat, ++i, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, ++i, startTime);
        this.setPrepareStatement(preStat, ++i, sessionContainer.getUserRecordID());
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt < pageSize) {
          AuditTrail tmpAuditTrail = new AuditTrail();
          tmpAuditTrail.setID(getResultSetInteger(rs, "ID"));
          tmpAuditTrail.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpAuditTrail.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpAuditTrail.setObjectName(getResultSetString(rs, "OBJECT_NAME"));
          tmpAuditTrail.setVersionNumber(getResultSetString(rs, "VERSION_NUMBER"));
          tmpAuditTrail.setAccessType(getResultSetString(rs, "ACCESS_TYPE"));
          tmpAuditTrail.setSessionID(getResultSetString(rs, "SESSION_ID"));
          tmpAuditTrail.setIpAddress(getResultSetString(rs, "IP_ADDRESS"));
          tmpAuditTrail.setAccessorID(getResultSetInteger(rs, "ACCESSOR_ID"));
          tmpAuditTrail.setAccessDate(getResultSetTimestamp(rs, "ACCESS_DATE"));
          tmpAuditTrail.setCreatorName(UserInfoFactory.getUserFullName(tmpAuditTrail.getCreatorID()));
          tmpAuditTrail.setUpdaterName(UserInfoFactory.getUserFullName(tmpAuditTrail.getUpdaterID()));
          tmpAuditTrail.setRecordCount(totalNumOfRecord);
          tmpAuditTrail.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpAuditTrail);
        }
        return (result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try {
          rs.close();
        } catch (Exception ignore) {} finally {
          rs = null;
        }
        try {
          preStat.close();
        } catch (Exception ignore) {} finally {
          preStat = null;
        }
        try {
          rsCnt.close();
        } catch (Exception ignore) {} finally {
          rsCnt = null;
        }
        try {
          preStatCnt.close();
        } catch (Exception ignore) {} finally {
          preStatCnt = null;
        }
      }
    }
  }
  
  public synchronized List getListByDocumentTypeNew(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MONTH,-1);
    String startTime = TextUtility.formatDate(cal.getTime(), SystemParameterFactory.getSystemParameter(SystemParameterConstant.DB_DATETIME_FORMAT));

    synchronized (dbConn) {
      try {
        sqlStat.append("SELECT DISTINCT A.OBJECT_TYPE, A.OBJECT_ID, A.ACCESS_DATE ");
        sqlStat.append("FROM   AUDIT_TRAIL A, DMS_DOCUMENT B ");
        sqlStat.append("WHERE  A.OBJECT_TYPE = ? AND A.OBJECT_ID = B.ID AND B.DOCUMENT_TYPE = ? AND B.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        
        sqlStat.append("AND (A.ACCESS_DATE >= ? ) ");    
        sqlStat.append("AND (A.ACCESSOR_ID = ? ) ");
        
        // Form the ORDER clause for sorting.
//        if (searchForm.isSortable()) {
//          String sortAttribute = searchForm.getSortAttribute();
//          if (sortAttribute.indexOf(".") < 0) {
//            sortAttribute = "A." + sortAttribute;
//          }
//          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
//        }
        
        sqlStat.append("ORDER BY ACCESS_DATE DESC");

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQLNew(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        int i = 0;
        this.setPrepareStatement(preStatCnt, ++i, GlobalConstant.OBJECT_TYPE_DOCUMENT);
        this.setPrepareStatement(preStatCnt, ++i, DmsDocument.FOLDER_TYPE);
        this.setPrepareStatement(preStatCnt, ++i, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStatCnt, ++i, startTime);
        this.setPrepareStatement(preStatCnt, ++i, sessionContainer.getUserRecordID());
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try {
          rsCnt.close();
        } catch (Exception ignore) {} finally {
          rsCnt = null;
        }
        try {
          preStatCnt.close();
        } catch (Exception ignore) {} finally {
          preStatCnt = null;
        }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
                                          ResultSet.CONCUR_READ_ONLY);
        i = 0;
        this.setPrepareStatement(preStat, ++i, GlobalConstant.OBJECT_TYPE_DOCUMENT);
        this.setPrepareStatement(preStat,++i,DmsDocument.FOLDER_TYPE);
        this.setPrepareStatement(preStat, ++i, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, ++i, startTime);
        this.setPrepareStatement(preStat, ++i, sessionContainer.getUserRecordID());
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt < pageSize) {
          AuditTrail tmpAuditTrail = new AuditTrail();
//          tmpAuditTrail.setID(getResultSetInteger(rs, "ID"));
          tmpAuditTrail.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpAuditTrail.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
//          tmpAuditTrail.setVersionNumber(getResultSetString(rs, "VERSION_NUMBER"));
//          tmpAuditTrail.setAccessType(getResultSetString(rs, "ACCESS_TYPE"));
//          tmpAuditTrail.setSessionID(getResultSetString(rs, "SESSION_ID"));
//          tmpAuditTrail.setIpAddress(getResultSetString(rs, "IP_ADDRESS"));
//          tmpAuditTrail.setAccessorID(getResultSetInteger(rs, "ACCESSOR_ID"));
//          tmpAuditTrail.setAccessDate(getResultSetTimestamp(rs, "ACCESS_DATE"));
//          tmpAuditTrail.setCreatorName(UserInfoFactory.getUserFullName(tmpAuditTrail.getCreatorID()));
//          tmpAuditTrail.setUpdaterName(UserInfoFactory.getUserFullName(tmpAuditTrail.getUpdaterID()));
          tmpAuditTrail.setRecordCount(totalNumOfRecord);
          tmpAuditTrail.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpAuditTrail);
        }
        return (result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try {
          rs.close();
        } catch (Exception ignore) {} finally {
          rs = null;
        }
        try {
          preStat.close();
        } catch (Exception ignore) {} finally {
          preStat = null;
        }
        try {
          rsCnt.close();
        } catch (Exception ignore) {} finally {
          rsCnt = null;
        }
        try {
          preStatCnt.close();
        } catch (Exception ignore) {} finally {
          preStatCnt = null;
        }
      }
    }
  }
  
  public synchronized List getListByDocumentID(String docID) throws ApplicationException {
	    PreparedStatement preStat = null;
	    ResultSet rs = null;
	    StringBuffer sqlStat = new StringBuffer();
	    List result = new ArrayList();
	    synchronized (dbConn) {
	      try {
	        sqlStat.append("SELECT DISTINCT A.ID, A.OBJECT_TYPE, A.OBJECT_ID, A.OBJECT_NAME, A.VERSION_NUMBER, A.ACCESS_TYPE, A.SESSION_ID, A.IP_ADDRESS, A.ACCESSOR_ID, A.ACCESS_DATE ");
	        sqlStat.append("FROM   AUDIT_TRAIL A ");
	        sqlStat.append("WHERE  A.OBJECT_ID = ? AND A.ACCESS_TYPE = ? ");	        
	        sqlStat.append("ORDER BY ACCESS_DATE DESC");

	        preStat =  dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
	        this.setPrepareStatement(preStat, 1, docID);
	        this.setPrepareStatement(preStat, 2, AuditTrailConstant.ACCESS_TYPE_VIEW);
	        rs = preStat.executeQuery();

	        while (rs.next()) {
	          AuditTrail tmpAuditTrail = new AuditTrail();
	          tmpAuditTrail.setID(getResultSetInteger(rs, "ID"));
	          tmpAuditTrail.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
	          tmpAuditTrail.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
	          tmpAuditTrail.setObjectName(getResultSetString(rs, "OBJECT_NAME"));
	          tmpAuditTrail.setVersionNumber(getResultSetString(rs, "VERSION_NUMBER"));
	          tmpAuditTrail.setAccessType(getResultSetString(rs, "ACCESS_TYPE"));
	          tmpAuditTrail.setSessionID(getResultSetString(rs, "SESSION_ID"));
	          tmpAuditTrail.setIpAddress(getResultSetString(rs, "IP_ADDRESS"));
	          tmpAuditTrail.setAccessorID(getResultSetInteger(rs, "ACCESSOR_ID"));
	          tmpAuditTrail.setAccessDate(getResultSetTimestamp(rs, "ACCESS_DATE"));
	          tmpAuditTrail.setCreatorName(UserInfoFactory.getUserFullName(tmpAuditTrail.getCreatorID()));
	          tmpAuditTrail.setUpdaterName(UserInfoFactory.getUserFullName(tmpAuditTrail.getUpdaterID()));
	          result.add(tmpAuditTrail);
	        }
	        return (result);
	      } catch (ApplicationException appEx) {
	        throw appEx;
	      } catch (SQLException sqle) {
	        log.error(sqle, sqle);
	        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
	      } catch (Exception e) {
	        log.error(e, e);
	        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
	      } finally {
	        try {
	          rs.close();
	        } catch (Exception ignore) {} finally {
	          rs = null;
	        }
	        try {
	          preStat.close();
	        } catch (Exception ignore) {} finally {
	          preStat = null;
	        }
	        
	      }
	    }
	  }
  

  /**
   * Get audit trail list by documents ids.
   * 
   * @param searchForm
   * @param objectIDs
   * @return
   * @throws ApplicationException
   */
  public synchronized List getList(AbstractSearchForm searchForm, List objectIDs) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());
    ListDmsAuditTrailForm actionForm = (ListDmsAuditTrailForm)searchForm;

    synchronized (dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.OBJECT_TYPE, A.OBJECT_ID, A.OBJECT_NAME, A.VERSION_NUMBER, A.ACCESS_TYPE, A.SESSION_ID, A.IP_ADDRESS, A.ACCESSOR_ID, A.ACCESS_DATE ");
        sqlStat.append("FROM   AUDIT_TRAIL A ");
        sqlStat.append("WHERE  A.OBJECT_TYPE = ? ");

        
        if (!Utility.isEmpty(objectIDs)){
          if (objectIDs.size()>0){
            sqlStat.append(" AND ( ");
          }
          for (int i = 0; i<objectIDs.size(); i++){
            sqlStat.append(" A.OBJECT_ID = ?  OR ");
          }
          if (objectIDs.size()>0){
            sqlStat.delete(sqlStat.length()-3, sqlStat.length()-1);
            sqlStat.append(" ) ");  
          }
        }

        if (DataSourceFactory.getDatabaseType() == DataSourceFactory.DB_ORACLE) {
          if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
            sqlStat.append("AND (A.ACCESS_DATE >=TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(actionForm.getAccessDateFrom()),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS') AND A.ACCESS_DATE <=TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(actionForm.getAccessDateTo()),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS') ) ");
          }
          else if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && Utility.isEmpty(actionForm.getAccessDateTo())) {
            sqlStat.append("AND (A.ACCESS_DATE >= TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(actionForm.getAccessDateFrom()),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS') ) ");
          }
          else if (Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
            sqlStat.append("AND (A.ACCESS_DATE <= TO_DATE('"+TextUtility.formatDate(this.parseTimestamp(TextUtility.dbDateAdder(actionForm.getAccessDateTo(),1)),"yyyy-MM-dd hh:mm:ss")+"','YYYY-MM-DD HH24:MI:SS')  ) ");
          }
        }else{
          if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
            sqlStat.append("AND (A.ACCESS_DATE >= '"+actionForm.getAccessDateFrom()+"' AND A.ACCESS_DATE <= '"+TextUtility.dbDateAdder(actionForm.getAccessDateTo(),1)+"' ) ");
          }else if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && Utility.isEmpty(actionForm.getAccessDateTo())) {
            sqlStat.append("AND (A.ACCESS_DATE >= '"+actionForm.getAccessDateFrom()+"' ) ");
          }else if (Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
            sqlStat.append("AND (A.ACCESS_DATE <= '"+TextUtility.dbDateAdder(actionForm.getAccessDateTo(),1)+"'  ) ");
          }
        }

        if (!Utility.isEmpty(actionForm.getAccessType())) {
          sqlStat.append("AND A.ACCESS_TYPE = ? ");
        }       
        
        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        int i = 0;
        this.setPrepareStatement(preStatCnt, ++i, actionForm.getObjectType());

        if (!Utility.isEmpty(objectIDs)){
          for (int x = 0; x<objectIDs.size(); x++){
            this.setPrepareStatement(preStatCnt, ++i, (Integer)objectIDs.get(x));
          }
        }

        if (!Utility.isEmpty(actionForm.getAccessType())) {
          this.setPrepareStatement(preStatCnt, ++i, actionForm.getAccessType());
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try {
          rsCnt.close();
        } catch (Exception ignore) {} finally {
          rsCnt = null;
        }
        try {
          preStatCnt.close();
        } catch (Exception ignore) {} finally {
          preStatCnt = null;
        }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
                                          ResultSet.CONCUR_READ_ONLY);
        i = 0;
        this.setPrepareStatement(preStat, ++i, actionForm.getObjectType());

        if (!Utility.isEmpty(objectIDs)){
          for (int x = 0; x<objectIDs.size(); x++){
            this.setPrepareStatement(preStat, ++i, (Integer)objectIDs.get(x));
          }
        }

        if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
          this.setPrepareStatement(preStat, ++i, actionForm.getAccessDateFrom());
          this.setPrepareStatement(preStat, ++i, TextUtility.dbDateAdder(actionForm.getAccessDateTo(),1));
        }
        else if (!Utility.isEmpty(actionForm.getAccessDateFrom()) && Utility.isEmpty(actionForm.getAccessDateTo())) {
          this.setPrepareStatement(preStatCnt, ++i, actionForm.getAccessDateFrom());
        }
        else if (Utility.isEmpty(actionForm.getAccessDateFrom()) && !Utility.isEmpty(actionForm.getAccessDateTo())) {
          this.setPrepareStatement(preStatCnt, ++i, TextUtility.dbDateAdder(actionForm.getAccessDateTo(),1));
        }

        if (!Utility.isEmpty(actionForm.getAccessType())) {
          this.setPrepareStatement(preStat, ++i, actionForm.getAccessType());
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt < pageSize) {
          AuditTrail tmpAuditTrail = new AuditTrail();
          tmpAuditTrail.setID(getResultSetInteger(rs, "ID"));
          tmpAuditTrail.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpAuditTrail.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpAuditTrail.setObjectName(getResultSetString(rs, "OBJECT_NAME"));
          tmpAuditTrail.setVersionNumber(getResultSetString(rs, "VERSION_NUMBER"));
          tmpAuditTrail.setAccessType(getResultSetString(rs, "ACCESS_TYPE"));
          tmpAuditTrail.setSessionID(getResultSetString(rs, "SESSION_ID"));
          tmpAuditTrail.setIpAddress(getResultSetString(rs, "IP_ADDRESS"));
          tmpAuditTrail.setAccessorID(getResultSetInteger(rs, "ACCESSOR_ID"));
          tmpAuditTrail.setAccessDate(getResultSetTimestamp(rs, "ACCESS_DATE"));
          tmpAuditTrail.setCreatorName(UserInfoFactory.getUserFullName(tmpAuditTrail.getCreatorID()));
          tmpAuditTrail.setUpdaterName(UserInfoFactory.getUserFullName(tmpAuditTrail.getUpdaterID()));
          tmpAuditTrail.setRecordCount(totalNumOfRecord);
          tmpAuditTrail.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpAuditTrail);
        }
        return (result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try {
          rs.close();
        } catch (Exception ignore) {} finally {
          rs = null;
        }
        try {
          preStat.close();
        } catch (Exception ignore) {} finally {
          preStat = null;
        }
        try {
          rsCnt.close();
        } catch (Exception ignore) {} finally {
          rsCnt = null;
        }
        try {
          preStatCnt.close();
        } catch (Exception ignore) {} finally {
          preStatCnt = null;
        }
      }
    }
  }

}
